class Stage extends Node {
    _time = 0;
    _canvas = null;
    constructor(canvas) {
        super();
        this._canvas = canvas;
    }
    start() {
        gl = getWebGLContext(this._canvas);
        gl.enable(gl.BLEND);
        gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
        this._loop(0);
    }
    _loop(time) {
        this.update((time - this._time) / 1000);
        this.draw();
        this._time = time;
        requestAnimationFrame(this._loop.bind(this));
    }
    update(dt) {
        for (let i = 0; i < this._children.length; i++) {
            this._children[i].update(dt);
        }
    }
    draw() {
        // 清空画布
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT);
        let shader = null;
        let vertices = null;
        let renderBatches = [];
        // 合批，收集顶点数据
        for (let i = 0; i < this._children.length; i++) {
            let c = this._children[i];
            if (!c.shader)
                continue;
            if (c.shader != shader) {
                shader = c.shader;
                vertices = [];
                renderBatches.push({
                    shader: shader,
                    vertices: vertices
                });
            }
            let c_vertices = c.getVertices();
            for (let t = 0; t < c_vertices.length; t++) {
                vertices.push(c_vertices[t]);
            }
        }
        // 渲染
        for (let i = 0; i < renderBatches.length; i++) {
            renderBatches[i].shader.bind(renderBatches[i].vertices);
            gl.drawArrays(gl.TRIANGLES, 0, renderBatches[i].vertices.length / renderBatches[i].shader.verticesNum);
        }
    }
}